//
//  MyComplainViewController.m
//  PropertyManager
//
//  Created by TNG on 2018/7/18.
//  Copyright © 2018年 Tng. All rights reserved.
//

#import "MyComplainViewController.h"

@interface MyComplainViewController () <UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) UITableView * tableview;
@property (nonatomic,assign) int page;
@property (strong, nonatomic) NSMutableArray * dataArr;
@property (nonatomic,assign) BOOL isHeadFresh;

@end

@implementation MyComplainViewController

static NSString * cellID = @"cell";
static NSString * MyComplainTimeTableViewCellID = @"MyComplainTimeTableViewCell";
static NSString * MyComplainContentTableViewCellID = @"MyComplainContentTableViewCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的投诉";
    self.page = 1;
    [self creatTableview];
    [self registCell];
    [self RequestData];
}

-(NSMutableArray *)dataArr
{
    if (_dataArr == nil) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

#pragma mark - 请求数据
-(void)RequestData
{
    WeakSelf;
    NSString * urlStr = [NSString stringWithFormat:@"/rest/tsRecordController/tsRecordList?tsr_tel=&page=%d&pagesize=10",self.page];
    [[HTTPClient shareClient] requestWithPath:urlStr AndReturnBlock:^(NSDictionary *dic) {
        if (weakSelf.isHeadFresh) {
            [weakSelf.dataArr removeAllObjects];
        }
        NSArray * dataArr = dic[@"data"];
        for (NSDictionary * dics in dataArr) {
            MyComPlainModel * model = [MyComPlainModel modelWithDic:dics];
            [weakSelf.dataArr addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf.tableview reloadData];
        });
    } AndFailMessage:^(NSString *message, NSString *errorCode) {
        [SVProgressHUD ShowErrorMessage:message];
    }];
}

-(void)registCell
{
    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:cellID];
    [self.tableview registerClass:[MyComplainTimeTableViewCell class] forCellReuseIdentifier:MyComplainTimeTableViewCellID];
    [self.tableview registerClass:[MyComplainContentTableViewCell class] forCellReuseIdentifier:MyComplainContentTableViewCellID];
}

-(void)creatTableview{
    if (self.tableview == nil) {
        UITableView * tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, kStatusAndNavBarH, WIDTH, HEIGHT - kStatusAndNavBarH - kHomeLineH) style:UITableViewStyleGrouped];
        tableview.delegate = self;
        tableview.dataSource = self;
        if (@available(iOS 11.0, *)) {
            tableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
        }
        self.tableview = tableview;
        [self.view addSubview:self.tableview];
    }
    // 下拉刷新
    WeakSelf;
    self.tableview.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.isHeadFresh = YES;
        weakSelf.page = 1;
        [weakSelf RequestData];
        [weakSelf.tableview.mj_header endRefreshing];
    }];
    // 设置自动切换透明度(在导航栏下面自动隐藏)
    self.tableview.mj_header.automaticallyChangeAlpha = YES;
    
    self.tableview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        // 结束刷新
        weakSelf.isHeadFresh = NO;
        weakSelf.page += 1;
        [weakSelf RequestData];
        [weakSelf.tableview.mj_footer endRefreshing];
    }];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 15;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 15)];
    line.backgroundColor = SFMColor(239, 239, 244);
    return line;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (self.dataArr.count == 0) {
        return [UIView new];
    }
    
    MyComPlainModel * model = [self.dataArr objectAtIndex:section];
    if (![model.spJs isEqualToString:@"Y"]) {
        return [UIView new];
    }
    
    UIView * bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 60)];
    bgView.backgroundColor =SFMWhiteColor;
    
    UIButton * askBtn = [[UIButton alloc] initWithFrame:CGRectMake(WIDTH - 201, 14, 83, 32)];
    [askBtn setTitle:@"查看回复" forState:UIControlStateNormal];
    askBtn.titleLabel.font = font(14);
    [askBtn setTitleColor:SFMNavBarColor forState:UIControlStateNormal];
    askBtn.layer.borderColor = SFMNavBarColor.CGColor;
    askBtn.layer.borderWidth = 1;
    askBtn.layer.cornerRadius = 16;
    askBtn.tag = 1+section;
    [askBtn addTarget:self action:@selector(askBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:askBtn];

    UIButton * judge = [[UIButton alloc] initWithFrame:CGRectMake(WIDTH - 98, 14, 83, 32)];
    judge.backgroundColor = SFMNavBarColor;
    [judge setTitle:@"评价" forState:UIControlStateNormal];
    judge.titleLabel.font = font(14);
    judge.tag = section + 10000;
    [judge addTarget:self action:@selector(judgeClick:) forControlEvents:UIControlEventTouchUpInside];
    judge.layer.cornerRadius = 16;
    [bgView addSubview:judge];
    
    return bgView;
}

#pragma mark - 查看回复
-(void)askBtnClick:(UIButton *)btn{
    SeeAskViewController * ctr = [[SeeAskViewController alloc] init];
    [self.navigationController pushViewController:ctr animated:YES];
}

#pragma mark - 评价
-(void)judgeClick:(UIButton *)btn{
    JudgeViewController * ctr = [[JudgeViewController alloc] init];
    [self.navigationController pushViewController:ctr animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (self.dataArr.count == 0) {
        return 0.01;
    }
    
    MyComPlainModel * model = [self.dataArr objectAtIndex:section];
    if (![model.spJs isEqualToString:@"Y"]) {
        return 0.01;
    }
    return 60;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.dataArr.count == 0 ) {
        return 0.01;
    }
    MyComPlainModel * model = [self.dataArr objectAtIndex:indexPath.section];
    if (indexPath.row == 0) {
         return 40;
    }
     return 50 + model.contentHeight;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.dataArr.count == 0) {
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        return cell;
    }
    MyComPlainModel * model = [self.dataArr objectAtIndex:indexPath.section];
    if (indexPath.row == 0) {
        MyComplainTimeTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:MyComplainTimeTableViewCellID];
        [cell sendMyComPlainModel:model];
        return cell;
    }else
    {
        MyComplainContentTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:MyComplainContentTableViewCellID];
        [cell sendMyComPlainModel:model];
        return cell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
}

-(BOOL)hidesBottomBarWhenPushed
{
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
